# coding:utf8
import os
import pandas as pd
from keras import Sequential
from keras.layers import Dense, Dropout
from keras.losses import mse,binary_crossentropy
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from keras.utils.vis_utils import plot_model as plot
addr=r'D:\hiicy\documents\python_decision_tree\DT_test_data.csv'

os.environ['PATH'] += os.pathsep + r"D:\Program Files\graphiz\bin"

def read_data(addr):
    data = pd.read_csv(filepath_or_buffer=addr)
    test_prop = 0.2
    labels = data.iloc[:, -1]
    data = data.iloc[:, :-1]
    # 训练、测试分割比例=8:2
    return train_test_split(data, labels, test_size=test_prop, random_state=22)

x_train, x_test, y_train, y_test = read_data(addr)

def _grid_find_kera(model):
    param_test = {
        "epochs": range(2, 18, 3),
        "batch_size": range(8, 64, 18)
    }
    gsearch1 = GridSearchCV(estimator=model,
                            param_grid=param_test, scoring="roc_auc", cv=5)
    gsearch1 = gsearch1.fit(x_train.values, y_train.values)
    print(f'Best_score:{gsearch1.best_score_}  Best_param:{gsearch1.best_params_}')
    print(gsearch1.cv_results_.keys())


def create_model():
    model = Sequential()
    model.add(Dense(32,input_dim=5, activation="relu"))  # input_shape 输入的数据矩阵维度
    model.add(Dropout(rate=0.5, seed=22))
    model.add(Dense(3,kernel_initializer='random_uniform', activation="relu"))
    model.add(Dense(1,kernel_initializer='random_uniform', activation="sigmoid"))

    model.compile(optimizer='adadelta', loss=binary_crossentropy, metrics=['accuracy'])

    return model

model = KerasClassifier(build_fn=create_model,validation_split=0.3)
_grid_find_kera(model)
import cv2